home *** CD-ROM | disk | FTP | other *** search
- /*
- TableWidth XFCN v1.1
-
- ©1991 Apple Computer, Inc.; by Mike Byrne
-
- This is a kinda weird XFCN, but very cool. It takes a string of any length, and returns a string
- of the width specified. If the given string is too short, spaces are added. If the string is
- too long, it is truncated. In addition, the XFCN checks to make sure that the last character
- is a space, so that the table entries are all pretty...
-
- Form:
- TableWidth(<string>, <width>)
-
- # the MPW 3.2 build commands:
- C -b TableWidth.c -mbg off
- Link -w -t STAK -c WILD -rt XFCN=613 ∂
- -m ENTRYPOINT ∂
- -sg TableWidth ∂
- TableWidth.c.o ∂
- "{Libraries}HyperXLib.o" ∂
- "{Libraries}Runtime.o" ∂
- "{Libraries}Interface.o" ∂
- "{CLibraries}StdCLib.o" ∂
- -o "teststack"
- */
-
- #include <Types.h>
- #include <Packages.h>
- #include <string.h>
- #include <Memory.h>
- #include "HyperXCmd.h"
-
- #define NULL '\0'
- #define NIL 0L
-
- #define kNumParams 2
-
-
- /* prototypes */
- void ErrorBack(XCmdPtr paramPtr, char *message);
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
- void UnlockParams ( XCmdPtr paramPtr, short paramCount );
-
-
-
- pascal void EntryPoint(XCmdPtr paramPtr)
- {
- /* variable declarations */
- long width;
- char strWidth[101];
- char retString[101];
- long giveLen;
-
-
- /* move high and lock the parameters. */
- MoveLockParams(paramPtr, paramPtr->paramCount);
-
- /* check for copyright or syntax help request */
- if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
- ErrorBack(paramPtr, "v1.1, ©1991 Apple Computer, Inc.; by Mike Byrne");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
- ErrorBack(paramPtr, "TableWidth syntax is 'TableWidth(<string>, <width>)");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* not a copyright or help request. */
- /* check for correct number of parameters */
- if (paramPtr->paramCount != kNumParams) {
- ErrorBack(paramPtr, "Error: TableWidth syntax is 'TableWidth(<string>, <width>)'");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* figure out the width of the column. */
- strcpy(strWidth, (char*)*paramPtr->params[1]);
- c2pstr(strWidth);
- StringToNum(strWidth, &width);
- if ( (width < 1) || (width > 100) ) {
- ErrorBack(paramPtr, "Error: The width is too low or too high.");
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* check to see if we're wider than the spec. if so, truncate... */
- giveLen = strlen( (char*)*paramPtr->params[0] );
- if ( giveLen >= width) {
- strncpy(retString, (char*)*paramPtr->params[0], width);
- retString[width] = NULL;
- if (retString[width-1] != ' ') { retString[width-1] = ' '; }
- ErrorBack(paramPtr,retString);
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
- /* otherwise, copy the requisite number of spaces onto the end. */
- p2cstr(strWidth);
- strcpy(strWidth," ");
- strcpy(retString, (char*)*paramPtr->params[0]);
- strncat(retString, strWidth, (width-giveLen));
-
- ErrorBack(paramPtr,retString);
- UnlockParams(paramPtr, paramPtr->paramCount);
- return;
- }
-
-
-
-
-
-
- /* allocate and load up paramPtr->returnValue with a string
- -------------------------------------------------------- */
- void ErrorBack(XCmdPtr paramPtr, char *message)
- {
- Handle mesHnd;
-
- /*
- Allocate space for an error message.
- Copy the string into it.
- Return the handle to HyperCard.
- */
- mesHnd = NewHandle((long)(strlen(message)+1));
- if (mesHnd == nil) return;
- strcpy((char *)*mesHnd,message);
- paramPtr->returnValue = mesHnd;
- }
-
-
-
- /* move high and lock down all parameters
- ----------------------------------------------------------------------- */
- void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
- {
- short i;
-
- for(i=0; i <= paramCount-1; i++)
- {
- MoveHHi(paramPtr->params[i]);
- HLock(paramPtr->params[i]);
- }
- }
-
-
-
-
- /* unlock all parameter handles in the XCmdBlock
- --------------------------------------------- */
- void UnlockParams ( XCmdPtr paramPtr, short paramCount )
- { short i;
-
- for(i=0; i <= paramCount-1; i++)
- { HUnlock(paramPtr->params[i]);}
- }
-